import { ref, onBeforeUnmount } from "vue";

export default function (onChange) {
  const online = ref(true);
  const updateNetwork = () => {
    online.value = navigator.onLine;
    onChange && onChange(online.value);
  };

  window.addEventListener("online", updateNetwork);
  window.addEventListener("offline", updateNetwork);

  onBeforeUnmount(() => {
    window.removeEventListener("online", updateNetwork);
    window.removeEventListener("offline", updateNetwork);
  });

  return { online };
}
